<?php

/*
This example creates an image (using the GD2 library) and writes it to disk,
according to your configuration. Make sure that the file you specify has the
correct permissions set.

You can then link to this image from anywhere (your website, blog, a forum signature,
e-mails, comments, ...)
*/

// some configuration
// colors are RGBA
$C = array(
	'textColor1'  => Array(255, 255, 255, 0),
	'textColor2'  => Array(200, 200, 200, 0),
	'lineColor'   => Array(100, 100, 100, 0),
	'bgColor'     => Array(10, 10, 10, 10),
	'textSize1'   => 14,
	'textSize2'   => 10,
	'nothingSize' => 15,
	'textFont'    => './DejaVuSans.ttf',
	'saveFile'    => 'nowplaying.png',
	'nothingText' => 'Nothing currently playing.',
	'vPadding'    => 20,
	'hPadding'    => 20
);

require_once 'RTWUtils.class.php';

function startImage($width, $height) {
	global $C;
	$oImg = imagecreatetruecolor($width, $height);
	imagesavealpha($oImg, true);
	imagealphablending($oImg, true);
	$aBgColor = $C['bgColor'];
	$iBgColor = imagecolorallocatealpha($oImg, $aBgColor[0], $aBgColor[1], $aBgColor[2], $aBgColor[3]);
	imagefill($oImg, 1, 1, $iBgColor);
	return $oImg;
}
function getTextWidthHeight($aTextSizes) {
	return array ( $aTextSizes[2] - $aTextSizes[0], $aTextSizes[3] - $aTextSizes[5] );
}
function getTextHeightOffset($aTextSizes) {
	return -$aTextSizes[5];
}
function durationToString($seconds) {
	$seconds = (int) $seconds;
	$mins = floor($seconds/60);
	$secs = $seconds % 60;
	if ($secs < 10) $secs = '0' . $secs;
	return ($mins . ':' . $secs);
}

$npInfo = RTWUtils::getInfoFromRequest();

if ($npInfo === false) {
	$aSizes = imagettfbbox($C['nothingSize'], 0, $C['textFont'], $C['nothingText']);
	list($width, $height) = getTextWidthHeight($aSizes);
	$heightOffset = getTextHeightOffset($aSizes);
	$oImg = startImage($width + $C['hPadding'], $height + $C['vPadding']);
	$aTextColor = $C['textColor1'];
	$iTextColor = imagecolorallocatealpha($oImg, $aTextColor[0], $aTextColor[1], $aTextColor[2], $aTextColor[3]);
	imagettftext($oImg, $C['nothingSize'], 0, $C['hPadding']/2, $heightOffset + $C['vPadding']/2, $iTextColor, $C['textFont'], $C['nothingText']);
	imagepng($oImg, $C['saveFile']);
	exit;
}

$headerText = $npInfo['title'];
$headerCoords = imagettfbbox($C['textSize1'], 0, $C['textFont'], $headerText . durationToString($npInfo['duration']));
$headerSize = getTextWidthHeight($headerCoords);
$headerOffset = getTextHeightOffset($headerCoords);
$bodyText = '';
if (array_key_exists('artist', $npInfo)) $bodyText .= 'by ' . $npInfo['artist'] . "\n";
if (array_key_exists('album', $npInfo)) $bodyText .= 'from ' . $npInfo['album'] . "\n";
$bodyText = trim($bodyText);
$bodyCoords = imagettfbbox($C['textSize2'], 0, $C['textFont'], $bodyText);
$bodySize = getTextWidthHeight($bodyCoords);
$bodyOffset = getTextHeightOffset($bodyCoords);

$width = max($headerSize[0], $bodySize[0]) + $C['hPadding'];
$height = $headerSize[1] + $bodySize[1] + 10 + $C['vPadding'];

$oImg = startImage($width, $height);

// header
$iTextColor = imagecolorallocatealpha($oImg, $C['textColor1'][0], $C['textColor1'][1], $C['textColor1'][2], $C['textColor1'][3]);
imagettftext($oImg, $C['textSize1'], 0, $C['hPadding']/2, $headerOffset + $C['vPadding']/2, $iTextColor, $C['textFont'], $headerText);
$length = durationToString($npInfo['duration']);
$lengthCoords = imagettfbbox($C['textSize2'], 0, $C['textFont'], $length);
$lengthSize = getTextWidthHeight($lengthCoords);
$lengthOffset = getTextHeightOffset($lengthCoords);
$lengthYPos = $C['vPadding']/2 + ($headerSize[1] - $lengthSize[1])/2 + $lengthOffset;
$iTextColor = imagecolorallocatealpha($oImg, $C['textColor2'][0], $C['textColor2'][1], $C['textColor2'][2], $C['textColor2'][3]);
imagettftext($oImg, $C['textSize2'], 0, $width - $C['hPadding']/2 - $lengthSize[0], $lengthYPos, $iTextColor, $C['textFont'], $length);
$nextY = $C['vPadding']/2 + $headerSize[1];
// separator
$iLineColor = imagecolorallocatealpha($oImg, $C['lineColor'][0], $C['lineColor'][1], $C['lineColor'][2], $C['lineColor'][3]);
imageline($oImg, $C['hPadding']/2, $nextY + 5, $width - $C['hPadding']/2, $nextY + 5, $iLineColor);
// line 2
imagettftext($oImg, $C['textSize2'], 0, $C['hPadding']/2, $nextY + $bodyOffset + 10, $iTextColor, $C['textFont'], $bodyText);

imagepng($oImg, $C['saveFile']);

?>
